import request from '@/utils/request'
import axios from 'axios'
import store from '@/store'
export function export2Excel(columns, list, pageId) {
  require.ensure([], () => {
    const { export_json_to_excel } = require('../excel/Export2Excel')
    const tHeader = []
    const filterVal = []
    if (!columns) {
      return
    }
    columns.forEach(item => {
      tHeader.push(item.label)
      filterVal.push(item.prop)
    })
    const data = list.map(v => filterVal.map(j => v[j]))
    export_json_to_excel(tHeader, data, pageId)
  })
}
// 通过后端接口拿到文件流
const service = axios.create({
  baseURL: '/common',
  timeout: 20 * 1000, // 请求超时 20s
  headers: {
    'Authorization': store.getters.token,
    'Content-Type': 'application/octet-stream;charset=UTF-8',
    'responseType': 'blob' // 这一行是关键，拿到blob数据类型的文件
  }

})
// service.interceptors.request.use(
//   config => {
//     config.headers['Content-Type'] = 'application/json'
//     return config
//   },
//   error => {
//     console.log(error) // for debug
//     return Promise.reject(error)
//   }
// )
export function getBlob(url, parameter, method) {
  // return service.post(url, parameter)
  return request({
    url: url,
    parameter,
    method: method || 'post',
    data: method !== 'get' ? parameter : null,
    params: method === 'get' ? parameter : null,
    responseType: 'blob' // 这一行是关键，拿到blob数据类型的文件
  })
}
export function downloadFile(data, fileName, fileSuffix) {
  let fileTypeMime = '' // 文件 mime 类型，移动端必传，否则下载不成功；pc端可传可不传
  switch (fileSuffix) { // 获取后缀对应的 mime
    case 'png': fileTypeMime = 'image/png'; break
    case 'doc': fileTypeMime = 'application/msword'; break
    case 'docx': fileTypeMime = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; break
    case 'jpg': case 'jpeg': fileTypeMime = 'image/jpeg'; break
    case 'gif': fileTypeMime = 'image/gif'; break
    case 'svg': fileTypeMime = 'image/svg+xml'; break
    case 'tif': case 'tiff': fileTypeMime = 'image/tiff'; break
    case 'txt': fileTypeMime = 'text/plain'; break
    case 'ppt': fileTypeMime = 'application/vnd.ms-powerpoint'; break
    case 'pptx': fileTypeMime = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'; break
    case 'xls': fileTypeMime = 'application/vnd.ms-excel'; break
    case 'xlsx': fileTypeMime = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; break
    case 'zip': fileTypeMime = 'application/zip'; break
    case '7z': fileTypeMime = 'application/x-7z-compressed'; break
    case '.pdf': fileTypeMime = 'application/octet-stream'; break
  }
  console.log('fileName', fileName)
  const blob = window.URL.createObjectURL(new Blob([data], {
    'type': fileTypeMime
  }))
  const link = document.createElement('a')
  link.style.display = 'none'
  link.href = blob

  link.setAttribute('download', fileName + fileSuffix)
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link) // 下载完成移除元素
  window.URL.revokeObjectURL(blob) // 释放掉 blob 对象
}
